//
//  ViewController.m
//  解析时间
//
//  Created by 伍银荣 on 2021/9/4.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()
@property (nonatomic, strong) NSNumber *addtime;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.addtime = @(1445327310);
   NSString *time = [self time:self.addtime];
    NSLog(@"time = %@",time);
}

- (NSString *)time:(NSNumber *)time {
    //把数字的日期转换成日期对象
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.addtime.intValue];
    NSLog(@"date = %@",date);
    //两个日期相减,获取指定的日期部分
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitMinute fromDate:date toDate:[NSDate date] options:0];
    if (components.minute < 60) {
        return [NSString stringWithFormat:@"%zd分钟以前",components.minute];
    }
    if (components.hour < 24) {
        return [NSString stringWithFormat:@"%zd小时以前",components.hour];
    }
    
    //获取日期
    NSDateFormatter *formetter = [[NSDateFormatter alloc]init];
    formetter.dateFormat = @"MM-dd HH:mm";
    return [formetter stringFromDate:date];
    
    
    
    
    
}
@end
